import renderEngine from "./renderEngine/engine1"

let pathSimplifierInsList = []
let pathSimplifierIns;

/**
 * 初始化
 * @param map
 */
export function addWorkPath(map) {
    AMapUI.load(['ui/misc/PathSimplifier'], function (PathSimplifier) {

        if (!PathSimplifier.supportCanvas) {
            alert('当前环境不支持 Canvas！');
            return;
        }
        //启动页面
        initPage(PathSimplifier, map);
    });
}

/**
 * 启动
 * @param PathSimplifier
 * @param map
 */
function initPage(PathSimplifier, map) {
    //创建组件实例
    pathSimplifierIns = new PathSimplifier({
        zIndex: 100,
        map: map, //所属的地图实例
        getPath: function (pathData) {
            return pathData.path;
        },
        getHoverTitle: function (pathData, pathIndex, pointIndex) {
            //返回鼠标悬停时显示的信息
            if (pointIndex >= 0) {
                //鼠标悬停在某个轨迹节点上
                // return pathData.name + '，点:' + pointIndex + '/' + pathData.path.length;
                return ""
            }
            //鼠标悬停在节点之间的连线上
            return `"${pathData.name}"的工作轨迹`
        },
        renderOptions: renderEngine
    });
    pathSimplifierInsList.push(pathSimplifierIns);
}

/**
 * 更新数据
 * @param name
 * @param path
 */
export function updateWorkPath(name, path) {
    let speed = 10;
    for (let i = 0; i < pathSimplifierInsList.length; i++) {
        if (path.length > 10) {
            renderEngine.pathTolerance = 10
            pathSimplifierInsList[i].renderOptions = renderEngine;
            speed = 100;
        }
        pathSimplifierInsList[i].setData([{
            name: name,
            path: path
        }]);
        //创建一个巡航器
        var navg0 = pathSimplifierInsList[i].createPathNavigator(0, //关联第1条轨迹
            {
                loop: true, //循环播放
                speed: speed
            });
        navg0.start();
    }
    pathSimplifierIns.show();
}

export function hidePath() {
    pathSimplifierIns.hide();
}